package com.azxcrm.project.system.basic.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.azxcrm.common.utils.StringUtils;
import com.azxcrm.project.system.basic.dao.IRegisterUserDao;
import com.azxcrm.project.system.basic.domain.RegisterUser;
import com.azxcrm.project.system.user.dao.IUserRoleDao;
import com.azxcrm.project.system.user.domain.UserRole;

/**
 * 注册用户(业务员) 服务层实现
 * 
 * @author Dustyone
 * @date 2018-11-07
 */
@Service
public class RegisterUserServiceImpl implements IRegisterUserService {
	@Autowired
	IUserRoleDao iUserRoleDao;

	@Autowired
	private IRegisterUserDao registerUserDao;

	/**
	 * 部门经理角色Key
	 */
	@Value("${crm.role.departmentManager}")
	private String departmentManager;

	/**
	 * 查询注册用户(业务员)信息
	 * 
	 * @param registerId
	 *            注册用户(业务员)ID
	 * @return 注册用户(业务员)信息
	 */
	@Override
	public RegisterUser selectRegisterUserById(Integer registerId) {
		return registerUserDao.selectRegisterUserById(registerId);
	}

	/**
	 * 查询注册用户(业务员)列表
	 * 
	 * @param registerUser
	 *            注册用户(业务员)信息
	 * @return 注册用户(业务员)集合
	 */
	@Override
	public List<RegisterUser> selectRegisterUserList(RegisterUser registerUser) {
		return registerUserDao.selectRegisterUserList(registerUser);
	}

	/**
	 * 新增注册用户(业务员)
	 * 
	 * @param registerUser
	 *            注册用户(业务员)信息
	 * @return 结果
	 */
	@Override
	public int insertRegisterUser(RegisterUser registerUser) {
		return registerUserDao.insertRegisterUser(registerUser);
	}

	/**
	 * 修改注册用户(业务员)
	 * 
	 * @param registerUser
	 *            注册用户(业务员)信息
	 * @return 结果
	 */
	@Override
	public int updateRegisterUser(RegisterUser registerUser) {
		return registerUserDao.updateRegisterUser(registerUser);
	}

	/**
	 * 保存注册用户(业务员)
	 * 
	 * @param registerUser
	 *            注册用户(业务员)信息
	 * @return 结果
	 */
	@Override
	public int saveRegisterUser(RegisterUser registerUser) {
		Integer registerId = registerUser.getRegisterId();
		int rows = 0;
		if (StringUtils.isNotNull(registerId)) {
			rows = registerUserDao.updateRegisterUser(registerUser);
		} else { // 密码加密
			/*registerUser.randomSalt();
			registerUser.setRegisterPassword(passwordService.encryptPassword(registerUser.getRegisterLoginname(),
					registerUser.getRegisterPassword(), registerUser.getRegisterSalt()));*/
			// 查询部门经理ID
			List<UserRole> userRoleInfo = iUserRoleDao.selectUserByRoleKeyByParam(departmentManager,
					registerUser.getRegisterDeptid());

			if (userRoleInfo.size() > 0 && userRoleInfo != null) {
				for (UserRole userRole : userRoleInfo) {
					registerUser.setRegisterAuditUserid(userRole.getUserId().intValue());
				}
			} else {
				registerUser.setRegisterAuditUserid(0);
			}
			rows = registerUserDao.insertRegisterUser(registerUser);
		}
		return rows;
	}

	/**
	 * 删除注册用户(业务员)信息
	 * 
	 * @param registerId
	 *            注册用户(业务员)ID
	 * @return 结果
	 */
	@Override
	public int deleteRegisterUserById(Integer registerId) {
		return registerUserDao.deleteRegisterUserById(registerId);
	}

	/**
	 * 批量删除注册用户(业务员)对象
	 * 
	 * @param registerIds
	 *            需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int batchDeleteRegisterUser(Integer[] registerIds) {
		return registerUserDao.batchDeleteRegisterUser(registerIds);
	}

}
